import { Controller, Get, Post, Body, Patch, Param, Delete, UseInterceptors, ExecutionContext, Query } from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { User } from './entities/user.entity';
import { ResultData } from 'src/common/response/result';

import { ResponsInterceptoreBase } from '../../../transform.interceptor';

import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { BaseController } from 'src/common/BaseController';
import { QueryListDto } from 'src/common/dto/';
import { CurrentUser, Public } from 'src/app.decorator';

@ApiTags('用户管理')
@Controller('user')
@UseInterceptors(ResponsInterceptoreBase)
export class UserController extends BaseController<User, UserService> {
  constructor(private readonly userService: UserService) {
    super(userService); // Pass the service to the base controller
  }

  @Get("list")
  @ApiOperation({ summary: '获取用户列表' })
  async GetUsers(@Query() query: any, @CurrentUser() user: any) {
    return this.Success(await this.userService.findAll(query, user.tenantId), "查询成功");
  }

  @Get('getUserInfo')
  @ApiOperation({ summary: '获取用户信息' })
  async getUserInfo( @CurrentUser() user: any) {
    console.log(user);
    return this.Success(user, "查询成功");
  } 

  @Get(':id')
  async findOne(@Param('id') id: string) {
    let result = await this.userService.findOne(id);
    
    if (result[0]) {
      return this.Success(result[1], "查询成功");
    } else {
      return this.Fail(result[1], "查询失败");
    }
  }

  @Post("UpdateUser")
  @ApiOperation({ summary: '用户更新' })
  async update(@Body() updateUserDto: CreateUserDto, @CurrentUser() currUser: any) {
    this.updateEntity(updateUserDto, currUser);
    console.log(updateUserDto);
    let result = await this.userService.update(updateUserDto);
    if(result[0]){
      return this.Success(result[1], "更新成功");
    }else{
      return this.Fail(result[1], "更新失败");
    }
  }

  @Delete('/delete/:id')
  @ApiOperation({ summary: '删除用户' })
  async remove(@Param('id') id: string) {
    let result = await this.userService.remove(id);
    if(result[0]){
      return this.Success(result[1], "删除成功");
    }else{
      return this.Fail(result[1], "删除失败");
    }
  }
  
  @Public()
  @Post('register')
  @ApiOperation({ summary: '用户注册' })
  @ApiResponse({ status: 200, description: '注册成功', type: User })
  async register(@Body() user: CreateUserDto) {
    return this.Success(await this.userService.register(user), "注册成功");
  }

  @Post('add')
  @ApiOperation({ summary: '新增用户' })
  @ApiResponse({ status: 200, description: '新增成功', type: User })
  async add(@Body() user: CreateUserDto, @CurrentUser() currUser: any) {
    this.InitEntity(user, currUser);
    return this.Success(await this.userService.register(user), "新增成功");
  }

  
}
